<?php
include_once("./common.php");

/***********调用*************/
$spread = new Spread();
$spread->cancelOrder();
$spread->cancelHpOrder();
$spread->cancelMicroStockOrder();
/***********************/

/**
 * 取消到期未支付的订单
 * @author 志杨
 *
 */
class Spread
{
	private $unit_time=24; //unit_time为取消与现在距离时间之前的所有满足条件的信息，单位为小时；
	
	/**
	 * DB实例
	 * @var dba
	 */
	private $db;
	
	private $config;
	
	/**
	 * 构造
	 */
	public function __construct()
	{
	    global $db,$config;
	    $this->db = $db;
	    $this->config = $config;
	}
	
	/**
	 * 取消普通订单
	 */
	public function cancelOrder(){
    	$d=$this->unit_time*3600;
    
    	$sql="select order_id from ".ORDER." where status=1 and UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(create_time)>=$d";
    	$this->db->query($sql); 
    	$re=$this->db->getRows();
    	if ($re){ 
    	    include_once($this->config["webroot"]."/module/product/includes/plugin_order_class.php");
    	    $order = new order;
    	    foreach ($re as $item){
    	        $order_statu = $order->set_order_statu($item['order_id'],0);
    	        $sql="update ".ORDER." set admin_remark='过期未付款自动取消' where order_id ='".$item['order_id']."'";
    	        $this->db->query($sql);
    	    }
    	}
	}
	
	/**
	 * 取消伙拼订单
	 */
	public function cancelHpOrder(){
	    $d=$this->unit_time*3600;
	    
    	$sql="select order_id from ".HUOPINORDER." where status=1 and UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(create_time)>=$d";
    	$this->db->query($sql);
    	$re=$this->db->getRows();
    	if ($re){
        	include_once($this->config['webroot']."/module/huopin/includes/plugin_huopin_class.php");
        	$huopin = new Huopin();
        	
    	    foreach ($re as $item){
    	        $huopin->cancelHpOrder($item['order_id'],'过期未付款自动取消');
    	    }
    	}
	}
	
	/**
	 * 取消微仓订单
	 */
	public function cancelMicroStockOrder(){
	    $d=$this->unit_time*3600;
	    
    	$sql="select order_id from ".MICROSTOCKORDER." where status=1 and UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(create_time)>=$d";
    	$this->db->query($sql);
    	$re=$this->db->getRows();
    	if ($re){
    	    include_once($this->config['webroot']."/module/micro_stock/includes/plugin_micro_stock_class.php");
    	    $microStock = new MicroStock();
    	    
    	    foreach ($re as $item){
    	        $microStock->cancelMicroOrder($item['order_id'],'过期未付款自动取消');
    	    }
    	}
	}
}
?>